In [2]:
import numpy as np
import pandas as pd
import keras
import tensorflow as tf
import os
from keras.models import Model
from keras.layers import Input, Conv2D, Flatten, MaxPooling2D, Dense, Dropout
from keras.optimizers import RMSprop
from keras.utils.np_utils import to_categorical
from keras import backend as K
from sklearn.model_selection import train_test_split
os.environ["CUDA_VISIBLE_DEVICES"] = '1'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
In [186]:
train_data = pd.read_csv('./facial-keypoints-detection/training.csv')
In [152]:
cols, rows = 96, 96
train_data.isnull().any()
#train_data.isnull().sum(axis=0)
#train_data = train_data.dropna()
#train_data.shape
Out[152]:
In [70]:
images = [np.array(list(map(int,image.split(' ')))).reshape(cols,rows)/255 for image in train_data['Image'].values]
In [71]:
images = np.array(images)
images = images.reshape(images.shape[0],cols,rows,1)
X_train = images
#y_train = train_data[['nose_tip_x','nose_tip_y']].values
y_train = train_data.drop(['Image'],axis=1).fillna(np.nan).values
#y_trains = y_train.reshape(y_train.shape[0],2)
print("Input Shape : {0}".format(X_train.shape))
print("Output Shape : {0}".format(y_train.shape))
In [57]:
pos = np.where(np.isnan(y_trains[:,1])==False)
y_trains[pos,1].transpose().shape
X_train[pos].shape
Out[57]:
In [106]:
def face_loss(y_true, y_pred):
# y = tf.constant([[1., np.nan], [np.nan, 2]])
# x = tf.constant([[1.1, 1], [0, 2.1]])
#_zeros = tf.zeros(y_pred.get_shape())
_zeros = tf.multiply(tf.constant(0.0),y_pred)
_y_true = tf.where(tf.is_nan(y_true),y_pred, y_true)
#_loss = tf.where(tf.is_nan(y_true),_zeros, tf.square(y_pred - y_true))
#return tf.reduce_mean(_loss, axis=-1)
return K.mean(K.square(y_pred - _y_true), axis=-1)
y = tf.constant([[1., np.nan], [np.nan, 2]])
x = tf.constant([[1.1, 1], [0, 2.1]])
with tf.Session() as sess:
print(sess.run(face_loss(y,x)))
In [145]:
def build_model(input_shape, output_shape):
input_tensor = Input(shape=input_shape)
out = Conv2D(filters = 64, kernel_size = (5,5),padding = 'Same', activation ='relu')(input_tensor)
out = Conv2D(filters = 64, kernel_size = (5,5),padding = 'Same', activation ='relu')(out)
out = MaxPooling2D(pool_size=(2,2))(out)
out = Dropout(0.25)(out)
out = Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu')(out)
out = Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='linear')(out)
out = MaxPooling2D(pool_size=(2,2), strides=(2,2))(out)
out = Dropout(0.25)(out)
out = Flatten()(out)
out = Dense(256, activation = "linear")(out)
out = Dropout(0.5)(out)
# out = Dense(128, activation = "linear")(out)
# out = Dropout(0.25)(out)
output_tensor = Dense(output_shape, activation = "linear")(out)
return input_tensor, output_tensor
In [146]:
input_shape = (cols, rows, 1)
output_shape = 30
#nb_features = 30
input_tensor, output_tensor = build_model(input_shape, output_shape)
#models = [Model(inputs=input_tensor, outputs=build_recognize(input_tensor, output_shape)) for i in range(nb_features)]
model = Model(inputs=input_tensor, outputs=output_tensor)
model.compile(
optimizer = RMSprop(lr=0.0005, rho=0.9, epsilon=1e-08, decay=0.0),
#loss = "categorical_crossentropy",
loss=face_loss,
metrics=["accuracy"]
)
# for i in range(nb_features):
# models[i].compile(
# optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0),
# #loss = "categorical_crossentropy",
# loss=face_loss,
# metrics=["accuracy"]
# )
In [147]:
random_seed = 2
_X_train, _X_val, _y_train, _y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=random_seed)
print("Train case {0}. Val case: {1}".format(len(_X_train),len(_X_val)))
In [148]:
history = model.fit(_X_train, _y_train, epochs=120, batch_size=128, validation_data=(_X_val, _y_val), verbose=1)
In [149]:
import matplotlib.pyplot as plt
%matplotlib inline
index = 1
# image = train_data['Image'].values[index]
# image = np.array(list(map(int,image.split(' ')))).reshape(cols,rows)
image = images[index]
y_trues = y_trains[index,:]
#y_preds = np.array([models[i].predict(X_train[index:index+1])[0] for i in range(nb_features)])
y_preds = model.predict(X_train[index:index+1])[0]
y_trues = y_trues.reshape(15,2)
y_preds = y_preds.reshape(15,2)
plt.imshow(image.reshape(cols,rows),plt.cm.gray)
plt.scatter(y_trues[:,0],y_trues[:,1],c='b')
plt.scatter(y_preds[:,0],y_preds[:,1],c='r')
Out[149]:
In [150]:
import matplotlib.pyplot as plt
import random
%matplotlib inline
cols_subplot, rows_subplot = 5,5
plt.figure(1,figsize=(20,20))
samples = random.sample(range(len(images)),cols_subplot*rows_subplot)
for index in range(1,cols_subplot*rows_subplot+1):
plt.subplot(cols_subplot,rows_subplot,index)
#index = 1
image = images[index]
y_trues = y_trains[index,:]
#y_preds = np.array([models[i].predict(X_train[index:index+1])[0] for i in range(nb_features)])
y_preds = model.predict(X_train[index:index+1])[0]
y_trues = y_trues.reshape(15,2)
y_preds = y_preds.reshape(15,2)
plt.imshow(image.reshape(cols,rows),plt.cm.gray)
plt.scatter(y_trues[:,0],y_trues[:,1],c='b')
plt.scatter(y_preds[:,0],y_preds[:,1],c='r')
In [151]:
import matplotlib.pyplot as plt
import random
%matplotlib inline
cols_subplot, rows_subplot = 5,5
plt.figure(figsize=(20,20))
index_samples = random.sample(range(len(images)),cols_subplot*rows_subplot)
for i in range(1,cols_subplot*rows_subplot+1):
plt.subplot(cols_subplot,rows_subplot,i)
#index = 1
index = index_samples[i-1]
image = images[index]
y_trues = y_train[index,:]
#y_preds = np.array([models[i].predict(X_train[index:index+1])[0] for i in range(nb_features)])
y_preds = model.predict(X_train[index:index+1])[0]
y_trues = y_trues.reshape(15,2)
y_preds = y_preds.reshape(15,2)
plt.imshow(image.reshape(cols,rows),plt.cm.gray)
plt.scatter(y_trues[:,0],y_trues[:,1],c='b')
plt.scatter(y_preds[:,0],y_preds[:,1],c='r')
In [100]:
y = tf.constant([[1., np.nan], [np.nan, 2]])
x = tf.constant([[1.1, 1], [0, 2.1]])
#_zeros = tf.zeros(x.get_shape())
_zeros = tf.multiply(tf.constant(0.0),x)
_loss = tf.where(tf.is_nan(y),_zeros, tf.square(x-y))
with tf.Session() as sess:
print(sess.run(_loss))
In [173]:
test_data = pd.read_csv('./facial-keypoints-detection/test.csv')
result = pd.read_csv('./facial-keypoints-detection/SampleSubmission.csv')
#result['Location'] = y_pred.astype(int)
ids = pd.read_csv('./facial-keypoints-detection/IdLookupTable.csv')
In [157]:
test_images = [np.array(list(map(int,image.split(' ')))).reshape(cols,rows)/255 for image in test_data['Image'].values]
In [161]:
test_images = np.array(test_images)
test_images = test_images.reshape(test_images.shape[0],cols,rows,1)
X_test = test_images
X_test.shape
Out[161]:
In [162]:
y_preds = model.predict(X_test)
In [168]:
y_preds
Out[168]:
In [191]:
df_y_preds = pd.DataFrame(columns=train_data.drop(['Image'],axis=1).keys(), data=y_preds)
In [184]:
ids.iloc[0]['ImageId']
Out[184]:
In [202]:
for i in range(ids.values.shape[0]):
image_index = ids.iloc[i]['ImageId'] - 1
feature_name = ids.iloc[i]['FeatureName']
value = df_y_preds.iloc[image_index][feature_name]
result.iloc[i]['Location'] = value
In [203]:
result
Out[203]:
In [204]:
result.to_csv('./facial-keypoints-detection/submission.csv',index=False)
In [ ]: